#! /usr/bin/env python
# -*- coding: utf-8 -*-

import operator

class Solution_slow:
    A = 'A'
    B = 'B'

    """
    @param S: a String consists of a and b
    @return: the longest of the longest string that meets the condition
    """
    def getAns(self, S):
        self.s = S
        self.len = len(S)
        self.cache = [(0,0)] * self.len     # cache[k], S[0] --> S[k] (a_cnts, b_cnts)
        self.max_cache = (0, 0, 0)        # a_b_cnt, [start, end)

        self._build_cache()
        self._find_max()

        return self.max_cache[0] * 2

    def _build_cache(self):
        if self.s[0] == self.A:
            self.cache[0] = (1,0)
        else:
            assert self.s[0] == self.B
            self.cache[0] = (0,1)
        last_a, last_b = self.cache[0]

        for idx in range(1, self.len):
            if self.s[idx] == self.A:
                self.cache[idx] = (last_a+1, last_b)
            else:
                assert self.s[idx] == self.B, "s[%s] must be '%s' or '%s'" % (idx, self.B, self.A)
                self.cache[idx] = (last_a, last_b+1)

            last_a, last_b = self.cache[idx]

    def _find_max(self):
        # 0也在begin中
        # [begin, begin) 空
        # [begin, begin+1) 只有一个元素, 'a'和'b'至少两个元素
        # 所以至少是begin + 2, 因为是开区间, 所以是begin + 1
        for begin in range(self.len):
            if not self._possible(begin):
                # print('hit first begin: %s' % begin)
                break

            for end in range(self.len, begin+1, -1):
                if not self._possible(begin, end):
                    # print('hit second, begin, end %s, %s' % (begin, end))
                    break

                is_same, cnt = self._get_a_b_cnt(begin, end)
                # if begin == 2 and end == 8:
                #     print('2, 8 : cnt %s' % cnt)
                self._try_refresh_max(cnt, begin, end, is_same)

    def _possible(self, begin, end=None):
        end = end or self.len

        return end - begin > self.max_cache[0] * 2

    # def minus(self, x, y):
    #     return x - y

    def _get_a_b_cnt(self, begin, end):
        assert end > begin
        if begin == 0:
            a, b = self.cache[end-1]
        else:
            a, b = map(operator.sub, self.cache[end-1], self.cache[begin-1])

        if a == b:
            return True, a
        else:
            return False, a

    def _try_refresh_max(self, cnt, begin, end, is_same=True):
        if not is_same:
            return False

        if cnt <= self.max_cache[0]:
            return False

        self.max_cache = cnt, begin, end


class Solution:
    A = 'A'
    B = 'B'

    def getAns(self, s):
        self.s = s
        self.len = len(self.s)

        self.dis_to_0 = [0] * self.len
        self.dis_map = {}    # dis_to_0 : least_idx + 1
        self.max = 0

        self._build_dis_to_0()
        self._find_max()

        print(len(self.dis_map))
        return self.max

    def _build_dis_to_0(self):
        # build dis_to_0
        self.dis_to_0[0] = self.dis(self.s[0])

        for idx in range(1, self.len):
            dis = self.dis(self.s[idx])
            self.dis_to_0[idx] = self.dis_to_0[idx-1] + dis

        # build dis_map
        self.dis_map[0] = 0  # 0 表示空第0个元素, 不存在

    def _find_max(self):
        for nth in range(1, self.len+1):
            dis_to_0 = self.dis_to_0[nth-1]

            if dis_to_0 not in self.dis_map:
                self.dis_map[dis_to_0] = nth
            else:
                another_nth = self.dis_map[dis_to_0]
                assert nth > another_nth
                if nth - another_nth > self.max:
                    self.max = nth - another_nth

    @classmethod
    def dis(kls, c):
        if c == kls.A:
            dis = 1
        else:
            dis = -1

        return dis


import sys
import os

DIR = os.path.dirname(os.path.abspath(__file__))
PARENT_DIR = os.path.dirname(DIR)
sys.path.append(PARENT_DIR)

import my_utils
from my_utils import time_decorator

@time_decorator
def main():
    print("start main")

    from data import data       # 990000 962916
    s = Solution()
    # s = Solution_slow()
    print(len(data))
    # data= 'ABABBA'
    r = s.getAns(data)
    # print(s.cache)
    # print(s.max_cache)
    print(r)


if __name__ == "__main__":
    main()
